package mylab.commons.web.mvc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import mylab.base.model.dto.PageData;
import mylab.base.model.dto.Result;
import mylab.base.model.entity.Entity;
import mylab.starters.mybatis.template.SuperService;
import mylab.starters.mybatis.template.dto.PageParam;
import mylab.starters.mybatis.template.dto.QueryParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.Serializable;
import java.util.List;

/**
 * controller~查询
 *
 * @param <K> 对应的entity的ID
 * @param <E> 对应的entity
 * @param <S> 对应的service
 * @param <D> 对应的dto
 */
public interface SelectController<
        K extends Serializable,
        E extends Entity<K>,
        S extends SuperService<K, E>,
        D>
        extends PageController<K, E, S, D> {


    @ApiOperation(value = "单体查询")
    @ApiOperationSupport(order = 41)
    @GetMapping(value = "/select/{id}")
    default Result<E> getById(@PathVariable K id) {
        return Result.newSuccess(getService().getById(id));
    }

    @ApiOperation(value = "列表查询")
    @ApiOperationSupport(order = 42)
    @PostMapping(value = "/select/list")
    default Result<List<E>> getList(@RequestBody @Validated QueryParam queryParam) {
        QueryWrapper<E> wrapper = queryParam.wrapper(getEntityClass());
        processListQueryWrapper(wrapper);
        return Result.newSuccess(getService().list(wrapper));
    }

    @ApiOperation(value = "分页查询")
    @ApiOperationSupport(order = 43)
    @PostMapping(value = "/select/page")
    default Result<PageData<E>> getPage(@RequestBody @Validated PageParam<D> pageParam) {
        IPage<E> page = pageQuery(pageParam);
        PageData<E> pageData = new PageData<>(
                (int) page.getTotal(),
                (int) page.getPages(),
                (int) page.getCurrent(),
                (int) page.getSize(),
                page.getRecords());

        return Result.newSuccess(pageData);
    }

    default void processListQueryWrapper(QueryWrapper<E> wrapper) {
    }

}
